package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.exception.MyException;
import com.itheima.common.result.JsonResult;
import com.itheima.common.utils.BaseContext;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import com.itheima.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhengbinghua
 * @since 2023-03-02
 */
@RestController
@RequestMapping("/shoppingCart")
@Api(tags = "购物车管理")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    /**
     * 加入购物车
     *
     * @return
     */
    @PostMapping("add")
    @ApiOperation(value = "添加购物车")
    public JsonResult addShoppingcart(@RequestBody ShoppingCart shoppingCart){
         //查询用户id
        String userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //封装添加
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //用户id是否存在
        wrapper.eq(ShoppingCart::getUserId,userId);
        //判断套餐?菜品是否存在购物车中
        String dishId = shoppingCart.getDishId();
       if (null!=dishId){//把菜品添加到购物车
           wrapper.eq(ShoppingCart::getDishId,dishId);
           //shoppingCart.setUserId();
       }else {//把套餐添加到购物车
           wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
       }
        ShoppingCart one = shoppingCartService.getOne(wrapper);
       if (null!=one){
           //存在数量+1
           Integer number = one.getNumber();
           shoppingCart.setNumber(number+1);
           //更新购物车数据
           shoppingCartService.updateById(one);
       }
       //不存在，数量设置为1,添加到购物车中
        shoppingCart.setNumber(1);
       shoppingCartService.save(shoppingCart);
       //新增对象赋值给one
       one=shoppingCart;
       return JsonResult.success("添加购物车成功！");
    }
/**
 * 清空购物车
 *
 * @return
 */
@ApiOperation("清空购物车")
@DeleteMapping("clean")
public JsonResult clearShoppingCart(){
    String id = BaseContext.getCurrentId();
    shoppingCartService.removeById(id);
    return JsonResult.success("删除购物车成功！");

}
/**
 * 查询购物车
 *
 * @return
 */
@GetMapping("list")
@ApiOperation("查询购物车" )
public JsonResult getShoppingCartList(){
    LambdaQueryWrapper<ShoppingCart> Wrapper = new LambdaQueryWrapper<>();
    String userId = BaseContext.getCurrentId();
    Wrapper.eq(ShoppingCart::getUserId,userId);
    List<ShoppingCart> list = shoppingCartService.list(Wrapper);
    return JsonResult.success(list);

}
/**
 * 删除购物车物品
 *
 * @return
 */
@ApiOperation(value = "删除购物车物品")
@PostMapping("sub")
public JsonResult subShoppingCart(@RequestBody ShoppingCart shoppingCart) {
    //判断购物车商品是否存在
    LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
    wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
    wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
    ShoppingCart one = shoppingCartService.getOne(wrapper);
    if (one == null) {//不存在报错
        throw new MyException(404, "购物车商品不存在！");
    }
    Integer number = one.getNumber();
    if (number>1){
       one.setNumber(number-1);
       //更新数据
        shoppingCartService.updateById(one);
    }
    //数量<1,清空购物车
    this.clearShoppingCart();
    return JsonResult.success("清空购物车成功！");
}

}
